package com.ourcraft.informationcollectionsystem.service.common.addQueryHistory;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ourcraft.informationcollectionsystem.entity.Company;
import com.ourcraft.informationcollectionsystem.entity.CompanyInfoHistory;
import com.ourcraft.informationcollectionsystem.entity.DataReportFormHistory;
import com.ourcraft.informationcollectionsystem.mapper.CompanyInfoHistoryMapper;
import com.ourcraft.informationcollectionsystem.mapper.CompanyMapper;
import com.ourcraft.informationcollectionsystem.mapper.DataReportFormHistoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AddHistoryServiceImpl implements AddHistoryService {
    @Autowired
    CompanyInfoHistoryMapper companyInfoHistoryMapper;
    @Autowired
    DataReportFormHistoryMapper dataReportFormHistoryMapper;
    @Autowired
    CompanyMapper companyMapper;

    @Override
    public void addFilingInfoSubmission(Integer companyId, Integer loggerId, Integer historyType, String historyComment, Boolean isCompanyId) {
        var userId = loggerId;
        if (isCompanyId)
            userId = getUserId(loggerId);
        var record = new CompanyInfoHistory(companyId, userId, historyType, historyComment);
        companyInfoHistoryMapper.insert(record);
    }

    @Override
    public void addFormSubmission(Integer formId, Integer loggerId, Integer historyType, String historyComment, Boolean isCompanyId) {
        var userId = loggerId;
        if (isCompanyId)
            userId = getUserId(loggerId);
        var record = new DataReportFormHistory(formId, userId, historyType, historyComment);
        dataReportFormHistoryMapper.insert(record);
    }

    private Integer getUserId(Integer companyId){
        QueryWrapper<Company> companyQuery = new QueryWrapper<>();
        companyQuery.eq("company_id", companyId);
        return companyMapper.selectOne(companyQuery).getUserId();
    }
}
